using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components.Web;
using SE.PO.CInterface.DBProtocols.Sites;
using SE.PO.CInterface.Web.Pages.DB.Room;

namespace SE.PO.CInterface.Web.Pages.DB.Site
{
    public partial class SiteTable
    {
        private AddSiteFrom _addFrom = default!;
        private EditSiteFrom _editFrom = default!;
        private DelSiteFrom _delFrom = default!;
       
        #region query

        List<SiteDto> _source = new();

        IEnumerable<SiteDto>? _selectedRows;

        ITable? _table;

        int _pageIndex = 1;
        int _pageSize = 10;
        int _total;

        protected override async Task OnInitializedAsync()
        {
            //todo:init areas
            //todo:init 局站类型
            //todo:init 局站特征
            await GetSourceAsync(_pageIndex, _pageSize);
        }

        public async Task OnChange(QueryModel<SiteDto> queryModel)
        {
            await GetSourceAsync(_pageIndex, _pageSize);
        }

        public async Task GetSourceAsync(int pageIndex, int pageSize)
        {
            try
            {
                var skipCount = (pageIndex - 1) * pageSize;
                var result = await Client.GetPage("", skipCount, pageSize);
                if (result != null)
                {
                    _source = result.Items.ToList();
                    _total = _source.Count;
                }
            }
            catch (System.Exception ex)
            {
                _ = Notice.Error(new()
                {
                    Duration = 100,
                    Message = "异常",
                    Description = ex.Message
                });
            }
           
            
        }

        #endregion

        #region add

        bool _addVisible;

        private void AddHandleOk(MouseEventArgs e)
        {
            _addFrom.Form.Submit();
        }

        private void AddHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _addVisible = false;
        }

        #endregion

        #region edit

        private CreateUpdateSiteDto? _editModel;
        bool _editVisible;

        private void EditHandleOk(MouseEventArgs e)
        {
            _editFrom.Form?.Submit();
        }

        private void EditHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _editVisible = false;
        }

        private void Edit(string id)
        {
            var dto = _source.FirstOrDefault(r => r.Id == id);
            if (dto != null)
            {
                _editModel = Mapper.Map<CreateUpdateSiteDto>(dto);
            }
        }

        #endregion

        #region delete

        bool _delVisible;
        private SiteDto? DelModel { get; set; } = new();


        private List<SiteDto>? DelModels { get; set; } = new();

        private void DeleteHandleOk(MouseEventArgs e)
        {
            _delFrom.Form?.Submit();
        }

        private void DeleteHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _delVisible = false;
        }

        private void DeleteSelectedRows()
        {
            DelModels = _selectedRows?.ToList();
            if (DelModels != null && DelModels.Any())
            {
                DelModel = DelModels.FirstOrDefault();
                _delVisible = true;
            }
        }

        private void Delete(string id)
        {
            DelModel = _source.FirstOrDefault(r => r.Id == id);
        }

        #endregion
    }
}